package gui;

import gui.options.CVOptionsManager;
import gui.options.TextManagerSettings;

import java.awt.CardLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;

/**This class is specifically meant to glue all the UI components together to make up the entire text area UI*/
public class ECMChannelManager extends JPanel {

	private static final long serialVersionUID = 1L;
	private ECMTextPane textPane = null;
	private TextManagerSettings settings = new TextManagerSettings(); //settings for anything that can be changed within this text manager; NOTE: these are *NOT* global UI settings
	private CVOptionsManager cvom = null;
	private JPanel switcher = new JPanel(new CardLayout());
	
	public ECMChannelManager() {
		this.setLayout(new GridBagLayout()); //set the layout manager
		GridBagConstraints constraints = new GridBagConstraints();
		
		this.textPane = new ECMTextPane(); //create my sub-components
		
		ChannelViewToolbar toolbar = new ChannelViewToolbar();
		addOptionsClickedAction(toolbar, this.switcher);
		
		this.cvom = new CVOptionsManager(toolbar, this.settings);
		
		this.switcher.add(this.textPane); //I want this one to be showing by default
		this.switcher.add(this.cvom);
		
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.gridx = 0;
		constraints.gridy = 0;
		constraints.gridheight = 1;
		constraints.weightx = 1.0;
		constraints.weighty = 0.0;
		this.add(toolbar, constraints); //add the toolbar
		
		this.textPane.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
		constraints.gridx = 0;
		constraints.gridy = 1;
		constraints.weighty = 1.0;
		constraints.fill = GridBagConstraints.BOTH;
		this.add(this.switcher, constraints);
	}
	
	/**add an action to the options button that flips the panels between showing the text and showing the options*/
	public void addOptionsClickedAction(final ChannelViewToolbar toolbar, final JPanel switcher) {
		toolbar.getOptionsButton().addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				CardLayout cl = (CardLayout) switcher.getLayout();
				cl.next(switcher);
			}
		});
	}
}
